package com.android.common.util;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.common.ActivityManager;
import com.android.common.R;

public class ToastUtil {

	private static final String TAG = "ToastUtil";

	public static void showToast(Context context, int res) {
		showToast(context, context.getResources().getString(res),
				Toast.LENGTH_SHORT);
	}

	public static void showToast(Context context, String msg) {
		showToast(context, msg, Toast.LENGTH_SHORT);
	}

	public static void showToast(Context context, String msg, int duration) {
		Context curActivity = ActivityManager.getInstance().getCurrentContext();
		if (curActivity != context) {
			Log.w(TAG, "showToast is fail by context=" + context
					+ ",curActivity=" + curActivity);
			return;
		}

		View mToastNormalMsg = LayoutInflater.from(context).inflate(
				R.layout.util_toast_normal_msg, null);
		TextView mToastNormalMsgTxt = (TextView) mToastNormalMsg
				.findViewById(R.id.toast_msg_txt);
		mToastNormalMsgTxt.setText(msg);
		Toast toast = Toast.makeText(context, msg, duration);
		toast.setView(mToastNormalMsg);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	public static void showToastMsg(String fromActivity, String msg) {
		String curActivity = ActivityManager.getInstance()
				.getCurrentContextName();
		if (curActivity.equals(fromActivity)) {
			ToastUtil.showToast(ActivityManager.getInstance()
					.getCurrentContext(), msg);
		} else {
			Log.w(TAG, "showToastMsg exec fail by curActivity=" + curActivity
					+ ",fromActivity=" + fromActivity);
		}
	}
}
